import parser
#import Genefinder.A as A
#import Genefinder.p as p
from Genefinder import *

import numpy
from numpy import savetxt

#Start- and end-codons.
startcodons = ['ATG','GTG','TTG']
endcodons = ['TAG','TGA','TAA']

def update(state, newstate, symbol):
     A[newstate, state] = A[newstate, state] + 1
     p[mapsymbol(symbol), newstate] = p[mapsymbol(symbol), newstate] + 1

def count(gnome, anot):
    gnome = parser.Parser(gnome)
    anot =  parser.Parser(anot)

    state = 0
    newstate = 0
    coding = anot.nextsymbol()

    while coding:
        if coding != 'C':
            newstate = 0

            symbol = gnome.nextsymbol()
            update(state, newstate, symbol)
            state = newstate
        else:
            symbol = gnome.nextsymbol()
            
            if state == 0:
                if symbol == 'A':
                    newstate = 1
                elif symbol == 'G':
                    newstate = 2
                elif symbol == 'T':
                    newstate = 3
                else: newstate = 0
            elif state == 1:
                if symbol == 'T':
                    newstate = 4
                else: newstate = 0
            elif state == 2:
                if symbol == 'T':
                    newstate = 4
                else: newstate = 0
            elif state == 3:
                if symbol == 'T':
                    newstate = 4
                else: newstate = 0
            elif state == 4:
                if symbol == 'G':
                    newstate = 5
                else: newstate = 0
            elif state == 5:
                newstate = 6
            elif state == 6:
                newstate = 7
            elif state == 7:
                newstate = 8
            elif state == 8:
                if anot.peek() == 'N':
                    newstate = 9
                else: newstate = 6
            elif state == 9:
                if symbol == 'A':
                    if gnome.peek(1) == 'G':
                        newstate = 10
                    elif gnome.peek(1) == 'A':
                        newstate = 12
                elif symbol == 'G':
                    newstate = 11
            elif state == 10:
                if symbol == 'G':
                    newstate = 13
            elif state == 11:
                if symbol == 'A':
                    newstate = 14
            elif state == 12:
                if symbol == 'A':
                    newstate = 15
            elif state == 13:
                newstate = 0
            elif state == 14:
                newstate = 0
            elif state == 15:
                newstate = 0
            else:
                raise ValueError("ran out of states... state: " + str(state))
            update(state,newstate,symbol)
            state = newstate

        coding = anot.nextsymbol()

#def cleanupMatrix(matrix):
#    for e in matrix.flat:
#         if e<100:
#              matrix.itemset(e,0)
    
def percentify(a, row):
    A  = numpy.arange(row * 16.0).reshape(row, 16.0)

    for i in range(16):
        sum = 0.0
        for j in range(row):
            sum += a[j,i]

        for j in range(row):
            A[j,i] = a[j,i] / sum

    return A


count("genome1.fa","annotation1.fa") 
count("genome2.fa","annotation2.fa") 
count("genome3.fa","annotation3.fa") 
count("genome4.fa","annotation4.fa") 
count("genome5.fa","annotation5.fa") 
count("genome6.fa","annotation6.fa") 
count("genome7.fa","annotation7.fa") 
count("genome8.fa","annotation8.fa") 
##count("genome9.fa","annotation9.fa") 
print A
print p

savetxt("A.txt", percentify(A,16), '%f', ',')
savetxt("p.txt", percentify(p,4), '%f', ',')
